﻿function radTreeWithTabStrip(treeViewId, tabStripId, multiPageId, loadingPanelId) {
    var radTreeView = $find(treeViewId), tabStrip = $find(tabStripId), multiPage = $find(multiPageId), loadingPanel = $find(loadingPanelId);
    $('#' + multiPageId + ' iframe').load(function () {
        loadingPanel.hide(multiPageId);
    });
    var me = this;
    me.radTreeView = radTreeView;
    me.tabStrip = tabStrip;
    me.multiPage = multiPage;
    me.loadingPanel = loadingPanel;
    var nodeCache = {}, winCache = {};

    radTreeView.add_nodeClicking(function (sender, args) {
        var node = args.get_node();
        if (node.get_nodes().get_count() > 0) {
            node.set_postBack(false);
            return;
        }
        me.addOrOpenTab(node);
    });

    this.createNode = function (url, text) {
        var v = (+new Date);
        var node = {
            get_value: function () { return v; },
            get_text: function () { return text; },
            get_attributes: function () {
                return {
                    getAttribute: function (p) {
                        return p == 'url' ? url : '';
                    }
                };
            }
        };
        nodeCache[url] = node;
        return node;
    };

    this.findAndOpenTab = function (url, text, win) {
        var node;
        winCache[url] = win;
        if (nodeCache[url])
            node = nodeCache[url];
        else
            node = me.createNode(url, text);
        me.addOrOpenTab(node);
        return node;
    };

    this.addOrOpenTab = function (node) {
        var val = node.get_value();
        var targetTab = tabStrip.findTabByValue(val);
        if (targetTab) {
            targetTab.set_selected(true);
        } else {
            loadingPanel.show(multiPageId);
            me.addTab(node);
        }
    };

    this.deleteTabByUrl = function (url) {
        if (nodeCache[url]) {
            var node = nodeCache[url];
            me.deleteTabByNode(node);
            var win = winCache[url];
            if (win && win.reloadData)
                win.reloadData();
        }
    };

    this.deleteTabByNode = function (node) {
        var val = node.get_value();
        var targetTab = tabStrip.findTabByValue(val);
        if (targetTab) {
            me.deleteTab(targetTab);
        }
    };

    this.deleteAllTab = function () {
        var tabs = tabStrip.get_tabs();
        var pageViews = multiPage.get_pageViews();
        var l = tabs.get_count();
        while (l-- > 1) {
            tabs.remove(tabs.getTab(l));
            pageViews.remove(pageViews.getPageView(l));
        }
        tabs.getTab(0).set_selected(true);
    };

    this.refreshActive = function () {
        var activeTab = tabStrip.get_selectedTab();
        var activeView = activeTab.get_pageView();
        var activeFrame = $('iframe', activeView.get_element());
        activeFrame.attr('src', activeFrame.attr('src'));
    };

    this.deleteTab = function (tab) {
        var pageView = tab.get_pageView();

        var tabToSelect = tab.get_nextTab();
        if (!tabToSelect)
            tabToSelect = tab.get_previousTab();

        tabStrip.get_tabs().remove(tab);
        multiPage.get_pageViews().remove(pageView);

        if (tabToSelect)
            tabToSelect.set_selected(true);
    };

    this.addTab = function (node) {
        var attrs = node.get_attributes();
        var tab = new Telerik.Web.UI.RadTab();
        tab.set_text(node.get_text());
        tab.set_value(node.get_value());
        tabStrip.get_tabs().add(tab);
        me.AttachCloseImage(tab);
        tab.set_imageUrl(attrs.getAttribute('icon'));
        tab.set_selected(true);
        var pageView = new Telerik.Web.UI.RadPageView();
        pageView.set_id(node.get_value());
        pageView.set_selected(true);
        multiPage.get_pageViews().add(pageView);
        //pageView.set_contentUrl(attrs.getAttribute('url'));
        var pel = pageView.get_element();
        $(pel).html("<iframe src='" + attrs.getAttribute('url') + "' frameBorder='0' scrolling='no' width='100%' height='100%'></iframe>")
            .css({ 'height': '100%',
                'border': '0 none'
            })
        $('iframe', pel).load(function () {
            loadingPanel.hide(multiPageId);
        });
        tab.set_pageViewID(node.get_value());
        return false;
    };

    this.CreateCloseImage = function () {
        var closeImage = document.createElement("img");
        $(closeImage).attr({
            src: "/shared/img/icons/delete_tab.gif",
            border: 0,
            alt: "关闭"
        }).css("vertical-align", "middle");
        return closeImage;
    }

    this.AttachCloseImage = function (tab) {
        var closeImage = me.CreateCloseImage();
        closeImage.AssociatedTab = tab;
        closeImage.onclick = function (e) {
            if (!e) e = event;
            if (!e.target) e = e.srcElement;

            me.deleteTab(tab);

            e.cancelBubble = true;
            if (e.stopPropagation) {
                e.stopPropagation();
            }

            return false;
        }
        tab.get_innerWrapElement().appendChild(closeImage);
    }
}